package com.piggy.logi.sys.controller;

import com.piggy.common.core.domain.R;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.log.annotation.LogiLog;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.bo.*;
import com.piggy.logi.sys.service.*;
import com.piggy.logi.sys.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(value = "运营管理控制器", tags = {"运营管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/logi/operation/dispatch")
public class LogiOperationController {

    @Resource
    private ILogiWaybillService logiWaybillService;
    @Resource
    private ILogiScheduleService logiScheduleService;
    @Resource
    private ILogiScheduleLogService logiScheduleLogService;
    @Resource
    private ILogiScheduleNodeService logiScheduleNodeService;
    @Resource
    private ILogiWaybillLoadService logiWaybillLoadService;
    @Resource
    private ILogiWaybillLogService logiWaybillLogService;

    @ApiOperation("运单详情-查询运单运行日志")
    @GetMapping("/waybillLogDetail/{waybillId}")
    public R<List<LogiWaybillLogVo>> waybillLogDetail(@NotNull(message = "主键不能为空")
                                                           @PathVariable("waybillId") Long waybillId) {
        return R.ok(logiWaybillLogService.waybillLogDetail(waybillId));
    }

    @ApiOperation("车次详情-查询待发货运单列表")
    @PostMapping("/dispatchList")
    public TableDataInfo<LogiWaybillExtVo> dispatchList(@Validated @RequestBody OperationDispatchQueryBo queryBo) {
        queryBo.setDeptId(SecurityUtils.getDeptId());
        return logiWaybillService.dispatchList(queryBo);
    }

    @ApiOperation("车次详情-查询待发车次列表")
    @PostMapping("/dispatchCarList")
    public TableDataInfo<LogiScheduleExtVo> dispatchCarList(@Validated @RequestBody OperationDispatchCarQueryBo queryBo) {
        queryBo.setDeptId(SecurityUtils.getDeptId());
        return logiScheduleService.dispatchCarList(queryBo);
    }

    @ApiOperation("车次详情-装车/卸车")
    @LogiLog(title = "车次详情-装车", businessType = BusinessType.INSERT)
    @PostMapping("/load")
    public R<Void> load(@Validated @RequestBody LogiWaybillLoadEditBo editBo) {
        editBo.setDeptId(SecurityUtils.getDeptId());
        logiWaybillLoadService.load(editBo);
        return R.ok();
    }

    @ApiOperation("车次详情-取消装车/卸车")
    @LogiLog(title = "车次详情-取消装车/卸车", businessType = BusinessType.INSERT)
    @PostMapping("/cancelLoad/{type}/{loadId}")
    public R<Void> cancelLoad(@ApiParam(value = "类型(0装车1卸车)不能为空")  @PathVariable(value = "type") Integer type,
                              @ApiParam(value = "装车/卸车ID") @PathVariable(value = "loadId") Long loadId) {
        logiWaybillLoadService.cancelLoad(type, loadId);
        return R.ok();
    }

    @ApiOperation("车次详情-发车")
    @LogiLog(title = "车次详情-发车", businessType = BusinessType.INSERT)
    @PostMapping("/depart/{scheduleNo}")
    public R<Void> depart(@ApiParam(value = "发车批次")
                          @PathVariable(value = "scheduleNo") String scheduleNo) {
        logiScheduleService.depart(scheduleNo);
        return R.ok();
    }

    @ApiOperation("车次详情-取消发车")
    @LogiLog(title = "车次详情-取消发车", businessType = BusinessType.INSERT)
    @PostMapping("/cancelDepart/{scheduleNo}")
    public R<Void> cancelDepart(@ApiParam(value = "发车批次")
                                @PathVariable(value = "scheduleNo") String scheduleNo) {
        logiScheduleService.cancelDepart(scheduleNo);
        return R.ok();
    }

    @ApiOperation("车次详情-车次到达")
    @LogiLog(title = "车次详情-车次到达", businessType = BusinessType.INSERT)
    @PostMapping("/arrived/{scheduleNo}")
    public R<Void> arrived(@ApiParam(value = "发车批次")
                           @PathVariable(value = "scheduleNo") String scheduleNo) {
        logiScheduleService.arrived(scheduleNo);
        return R.ok();
    }

    @ApiOperation("车次详情-取消车次到达")
    @LogiLog(title = "车次详情-取消车次到达", businessType = BusinessType.INSERT)
    @PostMapping("/cancelArrived/{scheduleNo}")
    public R<Void> cancelArrived(@ApiParam(value = "发车批次")
                                 @PathVariable(value = "scheduleNo") String scheduleNo) {
        logiScheduleService.cancelArrived(scheduleNo);
        return R.ok();
    }

    @ApiOperation("车次详情-完成车次")
    @LogiLog(title = "车次详情-完成车次", businessType = BusinessType.INSERT)
    @PostMapping("/completed/{scheduleNo}")
    public R<Void> completed(@ApiParam(value = "发车批次")
                             @PathVariable(value = "scheduleNo") String scheduleNo) {
        logiScheduleService.completed(scheduleNo);
        return R.ok();
    }

    @ApiOperation("车次详情-取消调度")
    @LogiLog(title = "车次详情-取消调度", businessType = BusinessType.INSERT)
    @PostMapping("/cancelScheduled/{scheduleNo}")
    public R<Void> cancelScheduled(@ApiParam(value = "发车批次")
                                 @PathVariable(value = "scheduleNo") String scheduleNo) {
        logiScheduleService.cancelScheduled(scheduleNo);
        return R.ok();
    }

    @ApiOperation("车次详情-查询待发车次详情")
    @GetMapping("/scheduleCarDetail/{scheduleId}")
    public R<LogiScheduleExtVo> scheduleCarDetail(@NotNull(message = "发车批次Id")
                                                  @PathVariable("scheduleId") Long scheduleId) {
        return R.ok(logiScheduleService.queryById(scheduleId));
    }

    @ApiOperation("车次详情-查询待发车次详情-跟踪")
    @PostMapping("/scheduleCarTraceDetail/{scheduleId}")
    public R<LogiScheduleExtVo> scheduleCarTraceDetail(@NotNull(message = "发车批次Id")
                                                       @PathVariable("scheduleId") Long scheduleId) {
        return R.ok(logiScheduleService.queryById(scheduleId));
    }

    @ApiOperation("车次详情-查询待发车次详情-节点列表")
    @PostMapping("/scheduleCarNode/{scheduleId}")
    public R<List<LogiScheduleNodeVo>> scheduleCarNode(@NotNull(message = "发车批次Id")
                                                       @PathVariable("scheduleId") Long scheduleId) {
        return R.ok(logiScheduleNodeService.scheduleCarNodeDetail(scheduleId));
    }

    @ApiOperation("车次详情-查询待发车次详情-添加车次节点")
    @PutMapping("/addScheduleCarNode")
    public R<Void> addScheduleCarNode(@RequestBody LogiScheduleNodeEditBo editBo) {
        editBo.setDeptId(SecurityUtils.getDeptId());
        logiScheduleNodeService.insertBo(editBo);
        return R.ok();
    }

    @ApiOperation("车次详情-查询待发车次详情-删除车次节点")
    @LogiLog(title = "车次详情-删除车次节点", businessType = BusinessType.INSERT)
    @PostMapping("/removeScheduleCarNode/{id}")
    public R<Void> removeScheduleCarNode(@NotNull(message = "标记节点Id")
                                         @PathVariable("id") Long id) {
        logiScheduleNodeService.removeById(id);
        return R.ok();
    }

    @ApiOperation("车次详情-查询待发车次详情-装载")
    @PostMapping("/scheduleCarLoad")
    public TableDataInfo<LogiScheduleExtVo> scheduleCarLoad(@Validated @RequestBody OperationDispatchQueryBo queryBo) {
        queryBo.setDeptId(SecurityUtils.getDeptId());
        return logiScheduleService.scheduleCarLoad(queryBo);
    }

    @ApiOperation("车次详情-查询车次节点(车次id)-日志")
    @GetMapping("/scheduleCarLogDetail/{scheduleId}")
    public R<List<LogiScheduleLogVo>> scheduleCarLogDetail(@NotNull(message = "主键不能为空")
                                                           @PathVariable("scheduleId") Long scheduleId) {
        return R.ok(logiScheduleLogService.scheduleCarLogDetail(scheduleId));
    }

    @ApiOperation("车次详情-查询车次节点(车次号)-日志")
    @GetMapping("/scheduleCarLog/{scheduleNo}")
    public R<List<LogiScheduleLogVo>> scheduleCarLogDetail(@NotBlank(message = "车次号不能为空")
                                                           @PathVariable("scheduleNo") String scheduleNo) {
        return R.ok(logiScheduleLogService.scheduleCarLogDetail(scheduleNo));
    }

    @ApiOperation("库存管理-查询库存运单列表")
    @PostMapping("/stockList")
    public TableDataInfo<LogiWaybillExtVo> stockList(@Validated @RequestBody OperationStockQueryBo queryBo) {
        queryBo.setDeptId(SecurityUtils.getDeptId());
        return logiWaybillService.stockList(queryBo);
    }

    @ApiOperation("回单管理-查询回单运单列表")
    @PostMapping("/receiptList")
    public TableDataInfo<LogiWaybillExtVo> receiptList(@Validated @RequestBody OperationReceiptQueryBo queryBo) {
        queryBo.setDeptId(SecurityUtils.getDeptId());
        return logiWaybillService.receiptList(queryBo);
    }

    @ApiOperation("回单管理-回单操作")
    @LogiLog(title = "回单管理-回单操作", businessType = BusinessType.INSERT)
    @PostMapping("/receipt")
    public R<Void> receipt(@Validated @RequestBody OperationReceiptBo receiptBo) {
        receiptBo.setDeptId(SecurityUtils.getDeptId());
        logiWaybillService.receipt(receiptBo);
        return R.ok();
    }
}
